package com.platform.rbac.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.platform.rbac.annotation.RequiresPermissions;
import com.platform.rbac.annotation.RequiresRoles;
import com.platform.rbac.annotation.TokenCheck;
import com.platform.rbac.callback.RoleAndPermissionCallback;
import com.platform.rbac.jwt.JWTUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ExampleController  extends BaseController{


    /**
     * 用户登录
     * 登录成功后 为 admin 创建token
     * 不需要任何注解 无需拦截
     * @return
     */
    @PostMapping("login")
    public String login(){
        JSONObject data=new JSONObject();
        data.put("code",200);
        data.put("msg","login success");
        data.put("data", JWTUtil.createToken("admin"));
        return data.toJSONString();
    }

    /**
     * 获取用户信息
     * 需要验证 请求头的token是否有效
     * @return
     */
    @TokenCheck
    @GetMapping("info")
    public String info(){
        User user = getUser();
        JSONObject data=new JSONObject();
        data.put("code",200);
        data.put("msg","");
        data.put("data", "return use info");
        return data.toJSONString();
    }

    /**
     *  权限验证
     * 删除用户 需要 user:delete 权限
     * @return
     */
    @RequiresPermissions(value = "user:delete")
    @PostMapping("delete")
    public String delete(){
        JSONObject data=new JSONObject();
        data.put("code",200);
        data.put("msg","delete success");
        return data.toJSONString();
    }


    /**
     *  角色验证
     * 新增用户 需要 user 角色
     * @return
     */
    @RequiresRoles(value = "user")
    @PostMapping("add")
    public String add(){
        JSONObject data=new JSONObject();
        data.put("code",200);
        data.put("msg","add success");
        return data.toJSONString();
    }

}
